<?php
$pageTitle = "卡组详情";
$currentPage = "people-set";
if (!isset($_GET['set_id'])) {
    header("Location: rank.php");
    exit();
}

$set_id = $_GET['set_id'];
include('../resource/db/db.php');
include('../resource/navbar.php');

// 获取卡组信息
$sql = "SELECT sets.*, users.username AS creator 
        FROM sets 
        JOIN users ON sets.user_id = users.user_id 
        WHERE sets.set_id='$set_id' AND sets.is_deleted=FALSE";
$result = $conn->query($sql);
$set = $result->fetch_assoc();

// 获取卡片信息
$cards_sql = "SELECT * FROM cards WHERE set_id='$set_id' AND is_deleted=FALSE";
$cards_result = $conn->query($cards_sql);

// 检查用户是否已登录
$user_id = isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : null;
?>

<!DOCTYPE html>
<html>

<head>
    <title>卡组详情</title>
    <link rel="stylesheet" href="../resource/css/styles.css">
</head>

<body>
    <div class="container">
        <?php if ($set) : ?>
            <h2><?php echo htmlspecialchars($set['name']); ?> 卡组详情</h2>
            <p><?php echo htmlspecialchars($set['info']); ?></p>
            <p>创建者: <?php echo htmlspecialchars($set['creator']); ?></p>
            <?php
            echo "<a href='rank.php'>返回首页</a>";
            ?>
            <?php if ($user_id) : ?>
                <form method="post">
                    <button type="submit" name="add_to_set" class="btn btn-primary">添加到我的卡组</button>
                    <button type="submit" name="reward" class="btn btn-success">打赏</button>
                    <?php
                    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
                        if (isset($_POST['add_to_set'])) {
                            // 添加卡组到用户的卡组
                            $new_set_name = $set['name'] . " - 复制";
                            $insert_set_sql = "INSERT INTO sets (name, user_id, info, created_at, is_deleted, reward, is_copy) VALUES ('$new_set_name', '$user_id', '{$set['info']}', NOW(), 0, 0, 1)";
                            if ($conn->query($insert_set_sql)) {
                                // 获取新卡组的ID
                                $new_set_id = $conn->insert_id;
                                $insert_cards_sql = "INSERT INTO cards (title, content, set_id, user_id, card_id, created_at, is_deleted, updated_at) 
                                                    SELECT title, content, '$new_set_id', '$user_id',  card_id, NOW(), 0, NOW() FROM cards WHERE set_id='$set_id' AND is_deleted=FALSE";
                                if ($conn->query($insert_cards_sql)) {
                                    echo "<p>卡组已成功添加到您的卡组中。</p>";
                                } else {
                                    echo "<p>添加卡片时出错。</p>";
                                    echo "<p>错误信息：" . $conn->error . "</p>";
                                }
                            } else {
                                echo "<p>添加卡组时出错。</p>";
                                echo "<p>错误信息：" . $conn->error . "</p>";
                            }
                        } elseif (isset($_POST['reward'])) {
                            // 增加热度
                            $reward_sql = "UPDATE sets SET reward=reward+1 WHERE set_id='$set_id'";
                            if ($conn->query($reward_sql)) {
                                echo "<p>谢谢你的喜欢！热度+1，当前热度：" . ($set['reward'] + 1) . "</p>";
                            } else {
                                echo "<p>打赏失败，请重试。</p>";
                            }
                        }
                    }
                    ?>
                </form>
            <?php endif; ?>

            <h3>卡片列表</h3>
            <ul class="list-group">
                <?php if ($cards_result->num_rows > 0) : ?>
                    <?php while ($card = $cards_result->fetch_assoc()) : ?>
                        <li class="list-group-item">
                            <div class="card-container">
                                <div class="front">
                                    <h4>正面</h4>
                                    <p><?php echo htmlspecialchars($card['title']); ?></p>
                                </div>
                                <div class="back" style="padding-top:20px; padding-bottom:20px">
                                    <h4>反面</h4>
                                    <p><?php echo htmlspecialchars($card['content']); ?></p>
                                </div>
                            </div>
                        </li>
                    <?php endwhile; ?>
                <?php else : ?>
                    <li class="list-group-item">暂无卡片</li>
                <?php endif; ?>
            </ul>
        <?php else : ?>
            <p>卡组不存在或已被删除。</p>
        <?php endif; ?>
    </div>
</body>

</html>
